package cn.liccshop.core.service.shopGoodsComment.controller;


import cn.liccshop.core.entity.ShopGoods;
import cn.liccshop.core.entity.ShopGoodsComment;
import cn.liccshop.core.service.shopGoodsComment.service.IShopGoodsCommentService;
import cn.liccshop.core.utils.ObjectToOtherUtils;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.entity.Result;
import cn.liccshop.sys.exception.MySystemException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.liccshop.sys.base.MyActionController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 商品评论表 前端控制器
 *
 * @author LiCC
 * @since 2019-05-13
 */
@RestController
@RequestMapping("/shopGoodsComment")
public class ShopGoodsCommentController extends MyActionController<ShopGoodsComment> {

    @Autowired
    private IShopGoodsCommentService liShopGoodsCommentService;

    @Override
    protected IService<ShopGoodsComment> getLiService(){
        return liShopGoodsCommentService;
    }

    @RequestMapping(value = "/submit")
    public Result orderCommentSubimt(@RequestBody Map<String, Object> map) throws MySystemException {
        String userId = getUserInfoByToken().getUserId();
        if (StringUtils.isBlank(userId)){
            throw new MySystemException("用户未登录", Constants.UNAUTHEN);
        }
        String orderId = ObjectToOtherUtils.toString(map.get("orderId"));
        String goodsId = ObjectToOtherUtils.toString(map.get("goodsId"));
        String orderGoodsId = ObjectToOtherUtils.toString(map.get("orderGoodsId"));
        String productsId = ObjectToOtherUtils.toString(map.get("productsId"));
        String productsName = ObjectToOtherUtils.toString(map.get("productsName"));
        Integer points = ObjectToOtherUtils.toInteger(map.get("points"));
        String content = ObjectToOtherUtils.toString(map.get("content"));
        if (StringUtils.isBlank(content)||StringUtils.isBlank(orderId)||StringUtils.isBlank(goodsId)||StringUtils.isBlank(orderGoodsId)){
            /** 评论信息为空 **/
            throw new MySystemException("信息不合规", Constants.INVALID_PARM);
        }
        ShopGoodsComment shopGoodsComment = new ShopGoodsComment()
                .setGoodsId(goodsId)
                .setOrderId(orderId)
                .setMemberId(userId)
                .setPoints(points)
                .setContent(content)
                .setProductsId(productsId)
                .setProductsName(productsName);
        liShopGoodsCommentService.orderCommentSubimt(shopGoodsComment, orderGoodsId);
        return new Result().setCode(Constants.OK).setMessage("评价成功");
    }

    @RequestMapping(value = "/checkByPage")
    public Result getCommentInfoByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestBody Map<String, Object> map){
        map.put("beginIndex", (page-1)*size);
        map.put("endIndex", page*size);
        Map<String, Object> data = liShopGoodsCommentService.liccCommontInfoByPage(map);
        data.put("pages", page);
        data.put("size", size);
        return new Result().setCode(Constants.OK).setData(data).setMessage("获取信息成功");
    }

    /**
     * 前台获取评论信息
     * 只能选择一个条件
     * @param map
     * @return
     */
    @RequestMapping(value = "/getInfos")
    public Result getCommentInfos(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestBody Map<String, Object> map){
        String goodsId = ObjectToOtherUtils.toString(map.get("goodsId"));
        /** 是否最新排序， null-不是； 1-是 **/
        Integer isNew = ObjectToOtherUtils.toInteger(map.get("isNew"));
        /** 是否最赞排序， null-不是； 1-是 **/
        Integer isLike = ObjectToOtherUtils.toInteger(map.get("isLike"));
        /** 差评 - 1； 中评 - 2； 好评 - 3； 无 - null **/
        Integer isPoints = ObjectToOtherUtils.toInteger(map.get("points"));
        /** 初始化Wrapper **/
        Wrapper<ShopGoodsComment> wrapper = null;
        if (isNew != null){
            wrapper = new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).orderByDesc("create_date");
        } else if (isLike != null){
            wrapper = new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).orderByDesc("like_num");
        } else if (isPoints != null){
            if (isPoints == 1){
                wrapper = new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1).setPoints(1)).orderByDesc("create_date");
            } else if (isPoints == 2){
//                wrapper = new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).eq("points", 2).or().eq("points", 3).orderByDesc("create_date");
                wrapper = new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).and(w -> w.eq("points", 2).or().eq("points", 3)).orderByDesc("create_date");
            } else if (isPoints == 3){
                wrapper = new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).and(w -> w.eq("points", 4).or().eq("points", 5)).orderByDesc("create_date");
            } else{
                wrapper = new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).orderByDesc("create_date");
            }
        } else {
            wrapper = new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).orderByDesc("create_date");
        }

        IPage<ShopGoodsComment> iPage = new Page<>(page, size);
        liShopGoodsCommentService.page(iPage, wrapper);
//        List<ShopGoodsComment> comments = liShopGoodsCommentService.list(wrapper);
        Integer allNum = liShopGoodsCommentService.list(new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1))).size();
//        Integer goodNum = liShopGoodsCommentService.list(new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).eq("points", 4).or().eq("points", 5)).size();
        Wrapper<ShopGoodsComment> goodWrapper = new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).and(w -> w.eq("points", 4).or().eq("points", 5));
        Integer goodNum = liShopGoodsCommentService.list(goodWrapper).size();
//        Integer mediumNum = liShopGoodsCommentService.list(new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).eq("points", 2).or().eq("points", 3)).size();
        Wrapper<ShopGoodsComment> mediuWrapper = new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1)).and(w -> w.eq("points", 2).or().eq("points", 3));
        Integer mediumNum = liShopGoodsCommentService.list(mediuWrapper).size();
        Integer badNum = liShopGoodsCommentService.list(new QueryWrapper<>(new ShopGoodsComment().setGoodsId(goodsId).setDisabled(1).setPoints(1))).size();
        Map<String, Object> data = new HashMap<>();
        data.put("iPage", iPage);
        data.put("allNum", allNum==null?0:allNum);
        data.put("goodNum", goodNum==null?0:goodNum);
        data.put("mediumNum", mediumNum==null?0:mediumNum);
        data.put("badNum", badNum==null?0:badNum);
        return new Result().setCode(Constants.OK).setData(data).setMessage("获取信息成功");
    }

}

